package com.swagger.controller;

import com.swagger.bean.House;
import com.swagger.mapper.HouseMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/house")
@Api(value = "房屋接口",tags = {"房屋访问操作接口"})
public class HouseController {

    @Autowired
    HouseMapper houseMapper;


    @GetMapping("/findById")
    @ApiOperation(value = "获取房屋信息",httpMethod = "GET",response = House.class,notes = "根据id获取房屋信息")
    @ApiImplicitParam(name = "id",value = "房屋id",required = true,paramType = "query",dataType = "Integer")
    public House findById(int id){
        House house = houseMapper.findById(id);
        return house;
    }

    @GetMapping("/getHouseList")
    @ApiOperation(value = "获取房屋列表",httpMethod = "GET",response = House.class,notes = "获取所有房屋列表")
    public List getHouseList(){
        return houseMapper.findAll();
    }

    @PostMapping("/saveHouse")
    @ApiOperation(value = "保存房屋",httpMethod = "POST",response = House.class,notes = "保存房屋")
    public String saveHouse(@RequestBody @ApiParam(name="房屋对象",value="传入json格式",required=true) House house){
        houseMapper.save(house);
        return "success";
    }

    @ApiOperation(value="修改房屋", notes="修改房屋",httpMethod = "PUT",response = House.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="主键id",required=true,paramType="query",dataType="Integer"),
            @ApiImplicitParam(name="houseName",value="用户名称",required=true,paramType="query",dataType = "String"),
            @ApiImplicitParam(name="houseSize",value="用户密码",required=true,paramType="query",dataType = "String")
    })
    @PutMapping(value="/updateHouse")
    public String updateHouse(int id,String houseName,String houseSize){
        House house = new House();
        house.setHouseName(houseName);
        house.setHouseSize(houseSize);
        houseMapper.save(house);
        return "success!";
    }

    @ApiOperation(value="删除房屋", notes="根据用户的id来删除房屋",httpMethod = "DELETE",response = House.class)
    @ApiImplicitParam(name = "id", value = "房屋ID", required = true,paramType = "query", dataType = "Integer")
    @DeleteMapping(value="/deleteHouseById")
    public String deleteHouseById(@RequestParam(value = "id")int id){
        House house = houseMapper.findById(id);
        houseMapper.delete(house);
        return "success!";
    }
}
